#!/usr/bin/env python3

#==============================================================================
# Functionality
#==============================================================================
import pdb
import sys
import os
import re

# utility funcs, classes, etc go here.

def asserting(cond):
    if not cond:
        pdb.set_trace()
    assert(cond)

def has_stdin():
    return not sys.stdin.isatty()

def reg(pat, flags=0):
    return re.compile(pat, re.VERBOSE | flags)

#==============================================================================
# Cmdline
#==============================================================================
import argparse

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, 
    description="""
TODO
""")
     
parser.add_argument('--verbose',
    action="store_true",
    help="verbose output" )
     
parser.add_argument('-i', '--ignore-case',
    action="store_true",
    help="case insensitive deduplication" )
     
parser.add_argument('-0', '--nul',
    action="store_true",
    help=r"expect NUL (``\0'') characters as separators, instead of newlines." )
     
parser.add_argument('-f', '--filename',
    action="store_true",
    help="Dedup based on os.path.basename of the input" )
     
parser.add_argument('-v', '--invert',
    action="store_true",
    help="Inverted output, i.e. only print if the item was already seen" )

args = None

#==============================================================================
# Main
#==============================================================================

def run():
    seen = set()
    for arg in sys.stdin.buffer:
        item = arg.lower() if args.ignore_case else arg
        k = os.path.basename(item) if args.filename else item
        if args.invert:
          ok = k in seen
        else:
          ok = k not in seen
        seen.add(k)
        if ok:
            sys.stdout.buffer.write(item)
            sys.stdout.buffer.flush()

def main():
    try:
        global args
        if not args:
            args, leftovers = parser.parse_known_args()
            args.args = leftovers
        return run()
    except IOError:
        # http://stackoverflow.com/questions/15793886/how-to-avoid-a-broken-pipe-error-when-printing-a-large-amount-of-formatted-data
        try:
            sys.stdout.close()
        except IOError:
            pass
        try:
            sys.stderr.close()
        except IOError:
            pass

if __name__ == "__main__":
    main()

